import axios from '@/axiosConfig'
import router from "@/router/index";
import i18n from '@/i18n/i18n'
import {msg_info,msg_error} from '@/assets/js/common.js'
export default {
    post:async function(url,val){
        let res=await axios({
            headers:{
                "Content-Type": "application/json; charset=utf-8",
              },
            url:BASEURL_BASE+url,
            method:'post',
            data:JSON.stringify(val)
        })
        return new Promise((resolve, reject) => {
            if (res.data.status === '000000') {
              resolve(res)
            }
            else if(res.data.status === '777777'){
                msg_info(i18n.t('axios.error_authFailed'));
                router.push('/login')
            }
            else {
                msg_error(i18n.t('axios.error_Code')+"【"+res.data.status+"】"+res.data.message);
                reject(res)
            }
        })
    },
   
    get:async function(url,val){
        let res=await axios({
            headers:{
                "Content-Type": "application/json; charset=utf-8",
            },
            url:BASEURL_BASE+url,
            method:'get',
            params:val
        })
        return new Promise((resolve, reject) => {
            if (res.data.status === '000000') {
              resolve(res)
            }
            else if(res.data.status === '777777'){
                msg_info(i18n.t('axios.error_authFailed'));
                router.push('/login')
            }
            else {
                msg_info(i18n.t('axios.error_Code')+"【"+res.data.status+"】"+res.data.message);
                reject(res)
            }
        })
    },
    put:async function(url,val){
        let res=await axios({
            headers:{
                "Content-Type": "application/json; charset=utf-8",
            },
            url:BASEURL_BASE+url,
            method:'put',
            data:JSON.stringify(val),
        })
        return new Promise((resolve, reject) => {
            if (res.data.status === '000000') {
              resolve(res)
            }
            else if(res.data.status === '777777'){
                msg_info(i18n.t('axios.error_authFailed'));
                router.push('/login')
            }
            else {
                msg_info(i18n.t('axios.error_Code')+"【"+res.data.status+"】"+res.data.message);
                reject(res)
            }
        })
    },
    delete:async function(url,val){
        let res=await axios({
            headers:{
                "Content-Type": "application/json; charset=utf-8",
              },
            url:BASEURL_BASE+url,
            method:'delete',
            data:JSON.stringify(val),
        })
        return new Promise((resolve, reject) => {
            if (res.data.status === '000000') {
              resolve(res)
            }
            else if(res.data.status === '777777'){
                msg_info(i18n.t('axios.error_authFailed'))
                router.push('/login')
            }
            else {
                msg_info(i18n.t('axios.error_Code')+"【"+res.data.status+"】"+res.data.message);
                reject(res)
            }
        })
    },
    postXml:async function(url,val){
        let res=await axios({
            url:BASEURL_BASE+url,
            method:'post',
            headers: {
                'Content-Type': 'application/xml'
            },
            data:val
        })
        return new Promise((resolve, reject) => {
            resolve(res)
        })
    },
    postJSON:async function(url,val){
        let res=await axios({
            headers:{
                "Content-Type": "application/json; charset=utf-8",
              },
            url:BASEURL_BASE+url,
            method:'post',
            data:val
        })
        return new Promise((resolve, reject) => {
            resolve(res)
        })
    },
}
